package com.yuju.controller.group;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.GroupReturnCodeEnum;
import com.yuju.group.model.GroupModel;
import com.yuju.group.service.IGroupService;
import com.yuju.util.Utils;
import com.yuju.vo.group.GroupVO;
/**
 * 
 * 类名：GroupController  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:37:03  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("group")
public class GroupController extends BaseController<GroupVO, GroupModel> {
	@Autowired
	private IGroupService groupService;
	
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject jsonObject)   {
		GroupVO groupVO = jsonObject.toJavaObject(GroupVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(groupVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = groupService.pageList(convert(groupVO), groupVO.getPage(), groupVO.getRows());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject jsonObject) {
		GroupVO groupVO = jsonObject.toJavaObject(GroupVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(groupVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = groupService.save(convert(groupVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_SAVE_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_SAVE_FAILURE.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject jsonObject) {
		GroupVO groupVO = jsonObject.toJavaObject(GroupVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(groupVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Map<String, String> failSave = FieldValidator.validateAll(groupVO, "save", true);
		if (!failSave.isEmpty()) {
			return validateError(failSave);
		}
		Result result = new Result();
		int rows = groupService.updateSelective(convert(groupVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_UPDATE_FAILURE.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject jsonObject) {
		GroupVO groupVO = jsonObject.toJavaObject(GroupVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(groupVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		
		Result result = new Result();
		int rows = groupService.del(groupVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_DEL_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_DEL_FAILURE.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_DEL_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 重名验证
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月21日 下午8:37:31 <br/>
	 */
	@RequestMapping("validRepeatName")
	@ResponseBody
	public Result validRepeatName(@RequestBody JSONObject jsonObject) {
		GroupVO groupVO = jsonObject.toJavaObject(GroupVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(groupVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		
		Result result = new Result();
		boolean b = groupService.validRepeatName(groupVO.getId(),groupVO.getName());
		if (b) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}

	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject jsonObject) {
		String id = jsonObject.getString("groupId");
		GroupVO groupVO = jsonObject.toJavaObject(GroupVO.class);
		groupVO.setId(id);
		Result result = new Result();
		GroupModel GroupModel = groupService.find(convert(groupVO));
		if (Utils.isNotNull(GroupModel)) {
			result.setData(GroupModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GroupReturnCodeEnum.GROUP_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GroupReturnCodeEnum.GROUP_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
}
